package com.aube.rvi.util;

import android.content.Context;
import android.os.Build;
import android.os.Environment;

import java.io.File;

/**
 * Created by huyaonan on 16/9/5.
 */
public class StorageUtil {

    public static String cachePath;

    /**
     * Like {@link Build.VERSION#SDK_INT}, but in a place where it can be conveniently
     * overridden for local testing.
     */
    public static final int SDK_INT =
            (Build.VERSION.SDK_INT == 23 && Build.VERSION.CODENAME.charAt(0) == 'N') ? 24
                    : Build.VERSION.SDK_INT;

    public static final void initDiskCacheDir(Context context) {
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            File sdcard = Environment.getExternalStorageDirectory();
            if(sdcard.exists() && SDK_INT < 23) {
                init(sdcard);
                cachePath = sdcard.getPath() + Constant.AUBE_DIR;
                return;
            }
        }
        cachePath = context.getCacheDir().getPath();
    }

    private static void init(File sdcard) {
        File aubeDirectory = new File(sdcard + Constant.AUBE_DIR);
        if (!aubeDirectory.exists())
            aubeDirectory.mkdirs();
    }

}
